//
//  SinaHomeTableViewCell.swift
//  WeiBo01
//
//  Created by mac on 16/1/6.
//  Copyright © 2016年 Han. All rights reserved.
//

import UIKit
import SnapKit

class SinaHomeTableViewCell: UITableViewCell {

    //声明一个全局约束
    var bottomConstant: Constraint?
    
    //MVVM
    var statusViewModel: SinaStatusViewModel?{
        
        didSet{
            originalView.orginalViewModel = statusViewModel
            
            footerView.footerViewModel = statusViewModel
            
//            retweetView.backgroundColor = UIColor.purpleColor()
            
            bottomConstant?.uninstall()
            // 就是原创微博 -- 转发微博的试图应该隐藏
            if statusViewModel?.model?.retweeted_status == nil {
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstant = make.top.equalTo(originalView.snp_bottom).constraint
                })
                retweetView.hidden = true
                //就是转发微博 -- 转发微博的试图不隐藏
            }else{
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstant = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.hidden = false
                
                retweetView.retweetViewModel = statusViewModel
            }
        
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
//        originalView.backgroundColor = UIColor.greenColor()
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        //添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
//            make.bottom.equalTo(contentView.snp_bottom)
            make.width.equalTo(contentView.snp_width)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.width.equalTo(contentView.snp_width)
//            make.height.equalTo(20)
        }
        
        footerView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.width.equalTo(contentView.snp_width)
            make.height.equalTo(44)
            bottomConstant = make.top.equalTo(retweetView.snp_bottom).constraint
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(footerView.snp_bottom)
            make.width.equalTo(self.snp_width)
        }
    }
    
    //MARK-- 懒加载
 /// 原始视图
    lazy var originalView: SinaOriginalView = SinaOriginalView()
 /// 底部工具栏
    lazy var footerView: SinaFooterView = SinaFooterView()
 /// 转发视图
    lazy var retweetView: SinaRetweetView = SinaRetweetView()
    
}
